#pragma once
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <stdio.h>
#include <map>
#include <time.h>

#define SHARED_MEMORY "MumbleLink"
#define CHARACTER_FILE "Gw2MapsCharacters.txt"
#define INCHTOM(b) (b*39.3701)

typedef struct LinkedMem *PLinkedMem;


struct LinkedMem {
	UINT32	uiVersion;
	DWORD	uiTick;
	float	fAvatarPosition[3];
	float	fAvatarFront[3];
	float	fAvatarTop[3];
	wchar_t	name[256];
	float	fCameraPosition[3];
	float	fCameraFront[3];
	float	fCameraTop[3];
	wchar_t	identity[256];
	UINT32	context_len;
	unsigned char context[256];
	wchar_t description[2048];
};


struct characterComperator{
	bool operator()(const char *fst, const char *snd){
		return strcmp(fst, snd) < 0 ? true : false;
	}
};


enum profession{unknown, guardian, warrior, engineer, ranger, thief, elementalist, mesmer, necromancer};


class Position
{
public:
	Position(char *path);
	~Position(void);
	char* createSnapshot();
	bool initializeSharedMemory();
	void closeSharedMemory(){if(initialized){UnmapViewOfFile(memory); CloseHandle(hMemory); initialized = false;}}
	void setTracking(bool tracking){this->tracking = tracking;}
	void setCommander(bool commander){this->commander = commander;}
	float* getPosition(){return memory->fAvatarPosition;}
	void setGuildId(const char*);
	void setAccountName(const char*);
	void setChannelId(const char*);
	bool clearForUpdate();
	void setUpdateDistance(float distance){this->distance = distance;}
	void setUpdateTime(int time){updateTime = time;}
	bool characterChanged(){if(memcmp(memory->identity, image.identity, 256) != 0){memcpy(charData, image.identity, 256); return true;}return false;}
	char* getMessage();

private:
	PLinkedMem memory;
	LinkedMem image;
	HANDLE hMemory, updateThread;
	bool initialized, tracking, commander;
	float distance;
	clock_t lastUpdate;
	int updateTime;
	char *guildId, accountName[128], *channelId;
	WCHAR charData[256];

	void copyData(){memcpy(&image, memory, sizeof(struct LinkedMem));}
};

